/**
 * Author: luoqi
 * Created Date: 2025-11-12 01:09:56
 * Last Modified: 2025-11-17 22:05:17
 * Modified By: luoqi at <**@****>
 * Copyright (c) 2025 <*****>
 * Description:
 */

#include <adc.h>
#include "adc_port.h"

static uint16_t adc1_conv[5] = { 0 };

typedef struct {
    uint16_t ch2;
    uint16_t ch3;
    uint16_t ch4;
    uint16_t ch5;
    uint16_t ch6;

} AdcVal;

static AdcVal adc1_val;

int adc_start()
{
    HAL_ADC_Stop(&hadc1);
    HAL_ADCEx_Calibration_Start(&hadc1);
    HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc1_conv, 5);
    return 0;
}

int adc1_sampling_trigger()
{
    HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc1_conv, 5);
    return 0;
}

uint16_t adc_read(AdcPort port)
{
    switch(port) {
    case ADC1_CH2:
        return adc1_val.ch2;
    case ADC1_CH3:
        return adc1_val.ch3;
    case ADC1_CH4:
        return adc1_val.ch4;
    case ADC1_CH5:
        return adc1_val.ch5;
    case ADC1_CH6:
        return adc1_val.ch6;
    default:
        return 0;
    }
    return 0;
}

__weak void adc1_conv_cplt_cb_override()
{
    
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
    if(hadc == &hadc1) {
        adc1_val.ch2 = adc1_conv[0];
        adc1_val.ch3 = adc1_conv[1];
        adc1_val.ch4 = adc1_conv[2];
        adc1_val.ch5 = adc1_conv[3];
        adc1_val.ch6 = adc1_conv[4];
        adc1_conv_cplt_cb_override();
    }
}
